package com.free.mini.common.base;


import java.io.Serializable;

import com.alibaba.fastjson.JSON;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 返回数据 实体
 */
@Slf4j
@Data
public class ResultData<T extends Object> implements Serializable,ToJSON {
    private static final long serialVersionUID = 1L;
    public static Integer SUCCESS = ResultCode.SUCCESS.getCode();
    public static Integer FAIL = ResultCode.SYSTEM_ERROR.getCode();
    private Integer code;
    private T data;
    private String message;
    private String sign;
    private String traceId = "";
    private String randomKey;

    public ResultData(){
        this.code = ResultCode.SUCCESS.getCode();
        this.message = ResultCode.SUCCESS.getMessage();
        this.data = null;
    }
    public ResultData(ResultCode rc) {
        this.code = rc.getCode();
        this.message = rc.getMessage();
        this.data = null;
    }

    public ResultData(Integer code, T data) {
        this.code = code;
        this.message = null;
        this.data = data;
    }

    public ResultData(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResultData(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResultData(ResultCode rc, T data) {
        this.code = rc.getCode();
        this.message = rc.getMessage();
        this.data = data;
        toString();
    }


    public ResultData(ResultCode rc, String massage) {
        this.code = rc.getCode();
        this.message = massage;
    }

    public ResultData(ResultCode rc, String massage, T data) {
        this.code = rc.getCode();
        this.message = massage;
        this.data = data;
        toString();
    }

    public ResultData(ResultCode rc, T data, String sign) {
        this.code = rc.getCode();
        this.message = rc.getMessage();
        this.data = data;
        this.sign = sign;
        toString();
    }

    //成功直接返回
    public static <T> ResultData<T> ok() {
        ResultData<T> resultData = new ResultData<T>(ResultCode.SUCCESS);
        return resultData;
    }

    /**
     * 成功直接返回成功信息
     */
    public static <T> ResultData<T> ok(String message) {
        ResultData<T> resultData = new ResultData<T>(ResultCode.SUCCESS, message);
        return resultData;
    }

    /**
     * 成功直接返回成功信息和所带的数据
     */
    public static ResultData<Object> ok(String message, Object data) {
        ResultData<Object> resultData = new ResultData<Object>(ResultCode.SUCCESS, message, data);
        return resultData;
    }

    public static <T> ResultData<T> ok(T data, String sign) {
        return new ResultData<T>(ResultCode.SUCCESS, data, sign);
    }

    public static <T> ResultData<T> ok(T data) {
        return new ResultData<T>(ResultCode.SUCCESS, data);
    }

	public static <T> ResultData<T> data(T data) {
		return new ResultData<T>(ResultCode.SUCCESS, data);
	}

    public static <T> ResultData<T> error() {
        ResultData<T> resultData = new ResultData<T>(ResultCode.SYSTEM_ERROR);
        return resultData;
    }

    public static <T> ResultData<T> error(String message) {
        ResultData<T> resultData = new ResultData<T>(ResultCode.SYSTEM_ERROR, message);
        return resultData;
    }

    public static <T> ResultData<T> error(ResultCode resultCode, String message) {
        ResultData<T> resultData = new ResultData<T>(resultCode, message);
        return resultData;
    }

    public static <T> ResultData<T> error(ResultCode resultCode) {
        ResultData<T> resultData = new ResultData<T>(resultCode);
        return resultData;
    }

    public static <T> ResultData<T> error(int code,String message) {
        ResultData<T> resultData = new ResultData<T>(code, message);
        return resultData;
    }

    public static <T> ResultData<T> error(ResultCode resultCode, T data) {
        return new ResultData<T>(resultCode, data);
    }


    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return this.getCode() != null && this.getCode().equals(SUCCESS);
    }

    /**
     * 判断是否成功
     */
    public static boolean isSuccess(ResultData r) {
        if(r == null ) {
            return false;
        }
        return r.getCode() != null && r.getCode().equals(SUCCESS);
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
